//
//  HBAppLanguage.m
//  HeartBeat
//
//  Created by karong on 2023/9/8.
//

#import "HBAppLanguage.h"
#import "NSBundle+Language.h"
#import "AppDelegate.h"

static NSString *const UWUserLanguageKey = @"UWUserLanguageKey";

@implementation HBAppLanguage

+ (void)setUserLanguageType:(NSInteger)languageType {
    appConfig.languageType = languageType;
    NSArray * languageArray = @[@"",@"zh-Hans", @"en", @"zh-Hant", @""];
    NSString * language = languageArray[languageType];
    [HBAppLanguage setUserLanguage:language];
    [HBRequest 获取启动配置];
    
}

+ (void)setUserLanguage:(NSString *)userLanguage
{
    //跟随手机系统
    if (!userLanguage.length) {
        [self resetSystemLanguage];
        return;
    }
    //用户自定义
    [appUserDefaults setValue:userLanguage forKey:UWUserLanguageKey];
    [appUserDefaults setValue:@[userLanguage] forKey:@"AppleLanguages"];
    [appUserDefaults synchronize];
    [((AppDelegate*)([UIApplication sharedApplication].delegate)) reloadMain];
}

+ (NSString *)userLanguage
{
    return [appUserDefaults valueForKey:UWUserLanguageKey];
}

/**
 重置系统语言
 */
+ (void)resetSystemLanguage
{
    [appUserDefaults removeObjectForKey:UWUserLanguageKey];
    [appUserDefaults setValue:nil forKey:@"AppleLanguages"];
    [appUserDefaults synchronize];
}
@end
